package com.epfresh.adapter;

import android.graphics.Bitmap;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.DisplayImageOptions;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.assist.ImageScaleType;
import com.epfresh.api.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;
import com.epfresh.bean.StoreDetail;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class HomeViewPagerAdapter extends PagerAdapter/* implements View.OnClickListener */ {

    List<View> viewLists;
    ArrayList<StoreDetail> dCList = new ArrayList();
    private View.OnClickListener onItemClickListener;
    
    public void setOnItemClickListener(View.OnClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    
    public HomeViewPagerAdapter(List<View> lists, List<StoreDetail> dateLists) {
        viewLists = lists;
        dCList = (ArrayList<StoreDetail>) dateLists;
    }

    private int mChildCount = 0;

    @Override
    public void notifyDataSetChanged() {
        mChildCount = getCount();
        super.notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(Object object) {
        if (mChildCount > 0) {
            mChildCount--;
            return POSITION_NONE;
        }
        return super.getItemPosition(object);
    }

    @Override
    public int getCount() {                                                                 //获得size
        // TODO Auto-generated method stub
        return viewLists.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        // TODO Auto-generated method stub
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object)                       //销毁Item
    {

        container.removeView((View) object);
//        if (position >= viewLists.size() )
//            return;
//        view.removeView(viewLists.get(position));///
    }

    @Override
    public Object instantiateItem(ViewGroup view, int position)                                //实例化Item
    {
        //Log.e("__________", position + "");
        ViewPagerHolder vh = initViewHolder(viewLists.get(position));
        vh.ll_view_page.setVisibility(View.VISIBLE);

        vh.ll_address.setOnClickListener(onItemClickListener);
        vh.ll_order.setOnClickListener(onItemClickListener);
        vh.ll_dc.setOnClickListener(onItemClickListener);
        vh.ll_pre.setOnClickListener(onItemClickListener);

        vh.ll_address.setTag(R.id.item_key_position, "notDc");
        vh.ll_order.setTag(R.id.item_key_position, "notDc");
        vh.ll_dc.setTag(R.id.item_key_position, "notDc");
        vh.ll_pre.setTag(R.id.item_key_position, "notDc");
        vh.ll_address.setTag(R.id.item_key_id, position);
        vh.ll_order.setTag(R.id.item_key_id, position);
        vh.ll_dc.setTag(R.id.item_key_id, position);
        vh.ll_pre.setTag(R.id.item_key_id, position);
        if (position == 0) {
            //Log.e("__________", position + "");

            if (dCList == null || dCList.size() == 0) {
                vh.ll_dc.setTag(R.id.item_key_position, "add");

                view.addView(viewLists.get(position));
                return viewLists.get(position);
            } else {
                vh.tv_bulk_load.setText(dCList.get(0).getName());
                ImageLoader.getInstance().displayImage(dCList.get(0).getLogoUrl(), new ImageViewAware(vh.iv_normal),options);
                vh.ll_dc.setTag(R.id.item_key_position, dCList.get(0).getId());
                if("2".equals(dCList.get(0).getBindingStatus())){
                    vh.iv_mask_4.setImageResource(R.mipmap.home_applying);
                    vh.ll_dc.setClickable(false);
                }
            }
            view.addView(viewLists.get(position));
            return viewLists.get(position);
        }
        if ((position != 0) && (position != (viewLists.size() - 1))) {
            //Log.e("__________", position + "");
            vh.tv_my_order.setText(dCList.get(position * 4 - 3).getName());
            ImageLoader.getInstance().displayImage(dCList.get(position * 4 - 3).getLogoUrl(), new ImageViewAware(vh.iv_order),options);
            vh.ll_order.setTag(R.id.item_key_position, dCList.get(position * 4 - 3).getId());

            if("2".equals(dCList.get(position*4-3).getBindingStatus())){
                vh.iv_mask_1.setImageResource(R.mipmap.home_applying);
                vh.ll_order.setClickable(false);
            }


            vh.tv_pre.setText(dCList.get(position * 4 - 2).getName());
            ImageLoader.getInstance().displayImage(dCList.get(position * 4 - 2).getLogoUrl(), vh.iv_pre,options);
            vh.ll_pre.setTag(R.id.item_key_position, dCList.get(position * 4 - 2).getId());
            if("2".equals(dCList.get(position*4-2).getBindingStatus())){
                vh.iv_mask_2.setImageResource(R.mipmap.home_applying);
                vh.ll_pre.setClickable(false);
            }


            vh.tv_address.setText(dCList.get(position * 4 - 1).getName());
            ImageLoader.getInstance().displayImage(dCList.get(position * 4 - 1).getLogoUrl(), vh.iv_address,options);
            vh.ll_address.setTag(R.id.item_key_position, dCList.get(position * 4 - 1).getId());
            if("2".equals(dCList.get(position*4-1).getBindingStatus())){
                vh.iv_mask_3.setImageResource(R.mipmap.home_applying);
                vh.ll_address.setClickable(false);
            }
            vh.tv_bulk_load.setText(dCList.get(position * 4).getName());///////////////
            ImageLoader.getInstance().displayImage(dCList.get(position * 4).getLogoUrl(), vh.iv_normal,options);
            vh.ll_dc.setTag(R.id.item_key_position, dCList.get(position * 4).getId());
            if("2".equals(dCList.get(position*4).getBindingStatus())){
                vh.iv_mask_4.setImageResource(R.mipmap.home_applying);
                vh.ll_dc.setClickable(false);
            }
            view.addView(viewLists.get(position));
            return viewLists.get(position);
        }
/*        Log.e("pppp","pos:"+position);
        Log.e("pppp","viewLists:"+viewLists.size());*/
        if (position == (viewLists.size() - 1)) {
            switch (dCList.size() % 4) {
                case 0:
                    vh.tv_my_order.setText(dCList.get(position * 4 - 3).getName());
                    ImageLoader.getInstance().displayImage(dCList.get(position * 4 - 3).getLogoUrl(), vh.iv_order,options);
                    vh.ll_order.setTag(R.id.item_key_position, dCList.get(position * 4 - 3).getId());
                    if("2".equals(dCList.get(position*4-3).getBindingStatus())){
                        vh.iv_mask_1.setImageResource(R.mipmap.home_applying);
                        vh.ll_order.setClickable(false);
                    }
                    vh.tv_pre.setText(dCList.get(position * 4 - 2).getName());
                    ImageLoader.getInstance().displayImage(dCList.get(position * 4 - 2).getLogoUrl(), vh.iv_pre,options);
                    vh.ll_pre.setTag(R.id.item_key_position, dCList.get(position * 4 - 2).getId());
                    if("2".equals(dCList.get(position*4-2).getBindingStatus())){
                        vh.iv_mask_2.setImageResource(R.mipmap.home_applying);
                        vh.ll_pre.setClickable(false);
                    }
                    vh.tv_address.setText(dCList.get(position * 4 - 1).getName());
                    ImageLoader.getInstance().displayImage(dCList.get(position * 4 - 1).getLogoUrl(), vh.iv_address,options);
                    vh.ll_address.setTag(R.id.item_key_position, dCList.get(position * 4 - 1).getId());
                    if("2".equals(dCList.get(position*4-1).getBindingStatus())){
                        vh.iv_mask_3.setImageResource(R.mipmap.home_applying);
                        vh.ll_address.setClickable(false);
                    }
                    vh.tv_bulk_load.setText("添加配送中心");
                    vh.iv_normal.setImageResource(R.mipmap.home_bulk_load);
                    vh.ll_dc.setTag(R.id.item_key_position, "add");


                    break;
                case 1:
                    vh.ll_address.setVisibility(View.GONE);
                    vh.ll_pre.setVisibility(View.GONE);
                    vh.ll_dc.setVisibility(View.GONE);
                    vh.tv_my_order.setText("添加配送中心");
                    vh.iv_order.setImageResource(R.mipmap.home_bulk_load);
                    vh.ll_order.setTag(R.id.item_key_position, "add");


                    break;
                case 2:

                    vh.ll_address.setVisibility(View.GONE);
                    vh.ll_dc.setVisibility(View.GONE);
                    vh.tv_my_order.setText(dCList.get(position * 4 - 3).getName());
                    ImageLoader.getInstance().displayImage(dCList.get(position * 4 - 3).getLogoUrl(), vh.iv_order,options);
                    vh.ll_order.setTag(R.id.item_key_position, dCList.get(position * 4 - 3).getId());
                    if("2".equals(dCList.get(position*4-3).getBindingStatus())){
                        vh.iv_mask_1.setImageResource(R.mipmap.home_applying);
                        vh.ll_order.setClickable(false);
                    }

                    vh.tv_pre.setText("添加配送中心");
                    vh.iv_pre.setImageResource(R.mipmap.home_bulk_load);
                    vh.ll_pre.setTag(R.id.item_key_position, "add");


                    break;
                case 3:
                    vh.ll_dc.setVisibility(View.GONE);
                    vh.tv_my_order.setText(dCList.get(position * 4 - 3).getName());
                    ImageLoader.getInstance().displayImage(dCList.get(position * 4 - 3).getLogoUrl(), vh.iv_order,options);
                    vh.ll_order.setTag(R.id.item_key_position, dCList.get(position * 4 - 3).getId());
                    if("2".equals(dCList.get(position*4-3).getBindingStatus())){
                        vh.iv_mask_1.setImageResource(R.mipmap.home_applying);
                        vh.ll_order.setClickable(false);
                    }
                    vh.tv_pre.setText(dCList.get(position * 4 - 2).getName());////////////////////
                    ImageLoader.getInstance().displayImage(dCList.get(position * 4 - 2).getLogoUrl(), vh.iv_pre,options);
                    vh.ll_pre.setTag(R.id.item_key_position, dCList.get(position * 4 - 2).getId());
                    if("2".equals(dCList.get(position*4-2).getBindingStatus())){
                        vh.iv_mask_2.setImageResource(R.mipmap.home_applying);
                        vh.ll_pre.setClickable(false);
                    }
                    vh.tv_address.setText("添加配送中心");
                    vh.iv_address.setImageResource(R.mipmap.home_bulk_load);
                    vh.ll_address.setTag(R.id.item_key_position, "add");
                    break;

            }
            view.addView(viewLists.get(position));
            return viewLists.get(position);
        }


        view.addView(viewLists.get(position));
        return viewLists.get(position);
    }

    ViewPagerHolder initViewHolder(View view) {
        ViewPagerHolder vh = new ViewPagerHolder();
        vh.ll_address = view.findViewById(R.id.ll_address);
        vh.ll_dc = view.findViewById(R.id.ll_dc_flag);
        vh.ll_order = view.findViewById(R.id.ll_order);
        vh.ll_pre = view.findViewById(R.id.ll_pre);
        vh.ll_view_page = view.findViewById(R.id.ll_view_page);
        vh.iv_address = (ImageView) view.findViewById(R.id.iv_address);
        vh.iv_normal = (ImageView) view.findViewById(R.id.iv_normal);
        vh.iv_order = (ImageView) view.findViewById(R.id.iv_order);
        vh.iv_pre = (ImageView) view.findViewById(R.id.iv_pre);

        vh.iv_mask_1 = (ImageView) view.findViewById(R.id.iv_mask_1);
        vh.iv_mask_2 = (ImageView) view.findViewById(R.id.iv_mask_2);
        vh.iv_mask_3 = (ImageView) view.findViewById(R.id.iv_mask_3);
        vh.iv_mask_4 = (ImageView) view.findViewById(R.id.iv_mask_4);
        vh.tv_address = (TextView) view.findViewById(R.id.tv_address);
        vh.tv_bulk_load = (TextView) view.findViewById(R.id.tv_bulk_load);
        vh.tv_my_order = (TextView) view.findViewById(R.id.tv_my_order);
        vh.tv_pre = (TextView) view.findViewById(R.id.tv_pre);
        view.setTag(vh);
        return vh;
    }

    class ViewPagerHolder {
        View ll_view_page;
        View ll_order;
        View ll_pre;
        View ll_address;
        View ll_dc;

        ImageView iv_mask_1;
        ImageView iv_mask_2;
        ImageView iv_mask_3;
        ImageView iv_mask_4;

        ImageView iv_order;
        ImageView iv_address;
        ImageView iv_pre;
        ImageView iv_normal;
        TextView tv_my_order;
        TextView tv_pre;
        TextView tv_address;
        TextView tv_bulk_load;
    }


    DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.store_default) //设置图片在下载期间显示的图片
            .showImageForEmptyUri(R.mipmap.store_default)//设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.mipmap.store_default)
            .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
            .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
            .considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
            .bitmapConfig(Bitmap.Config.RGB_565).build();

}
